package com.chaoyong.higo.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.activity.MainActivity;
import com.chaoyong.higo.activity.ProductDeTimeActivity;
import com.chaoyong.higo.activity.SearchActivity;
import com.chaoyong.higo.activity.SlidingMenuActivity;
import com.chaoyong.higo.adapter.NewsAdapter;
import com.chaoyong.higo.adapter.SlidingMenuLeftAdapter;
import com.chaoyong.higo.base.BaseFragment;
import com.chaoyong.higo.bean.DaoJSBean.DataEntity.ListEntity;
import com.chaoyong.higo.bean.ListEntity2;
import com.chaoyong.higo.bean.NewsAnnoBean;
import com.chaoyong.higo.bean.SharBean;
import com.chaoyong.higo.bean.ShowOrderEntity;
import com.chaoyong.higo.utils.DoubleClickExitHelper;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.Mlog;
import com.chaoyong.higo.utils.TransferTool;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.view.AutoScrollViewPager;
import com.chaoyong.higo.view.CustomProgressDialog;
import com.chaoyong.higo.view.MyScrollView;
import com.chaoyong.higo.view.MyScrollView.OnScrollLisener;
import com.chaoyong.higo.view.MyViewPager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.sun.org.apache.bcel.internal.generic.I2F;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.generic.ReturnaddressType;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * 
 * @author 丁晶
 * 
 */
public class FragHomePage extends BaseFragment implements OnClickListener, OnRefreshListener, OnItemClickListener {
	// 侧滑菜单
	private ImageView base_left_iv, base_right_iv;
	private SlidingMenu mSlidingMenu;
	private ListView slidingmenu_list;
	private SlidingMenuLeftAdapter slidingMenuLeftAdapter;

	// 浮动窗口

	private LinearLayout homppage_top2, homppage_top1;
	private RelativeLayout scroll_top;
	private MyScrollView mReboundScrollView;
	private int searchLayoutTop;
	private LinearLayout inInclude;
	private boolean flag = true;
	private int statusBarHeight = 0;
	private int topicBarHeight = 0;

	private RelativeLayout renqi, shengyu, zuixin, zongxu_down, zongxu_up;
	private TextView renqi_tv, shengyu_tv, zuixin_tv, zongxu_down_tv, zongxu_up_tv;

	private RecyclerView recommend_recycler;
	// private PullToRefreshScrollView fragmain_refresh;

	private DoubleClickExitHelper mDoubleClickExit;
	private ImageView fraghome_new, fraghome_share, fraghome_list;
	private ImageButton freghome_recommend_imabt, fragmain_shar_imgbt;
	private LinearLayout fragmain_new_imgbt;
	private ImageView shar1_img, shar3_img, shar2_img;
	private TextView shar1_tx, shar2_tx, shar3_tx;
	private AutoScrollViewPager main_vPager;
	private List<String> list;
	private LayoutInflater inflater;
	private List<View> views;
	private View item;
	private ImageView item_view;
	private List<ListEntity2> listr;
	private List<ListEntity> entities;
	private MyAdapters adapter;
	/** 最新揭晓 */
	private MyViewPager myViewPager;
	private NewsAdapter mAdapter;
	private ImageView xian1_img, xian2_img, xian3_img, xian4_img, xian5_img;

	private GridView news_lay;

	private ViewGroup group;
	private ImageView[] imageViews;

	private CustomProgressDialog dialog;

	private Fragment mFragment;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		// frag_home_page
		mView = inflater.inflate(R.layout.frag_home_page, container, false);
		mView.setFocusable(true);// 这个和下面的这个命令必须要设置了，才能监听back事件。
		mView.setFocusableInTouchMode(true);
		mView.setOnKeyListener(backlistener);
		return mView;
	}

	@Override
	public void findView() {
		base_right_iv = (ImageView) mView.findViewById(R.id.homepage_right_tv);
		base_left_iv = (ImageView) mView.findViewById(R.id.homepage_left_iv);

		// 侧滑
		slidingMenuLeftAdapter = new SlidingMenuLeftAdapter(mContext);
		// 浮动窗
		mReboundScrollView = (MyScrollView) mView.findViewById(R.id.myScrollView);
		inInclude = (LinearLayout) mView.findViewById(R.id.viewInScroll);
		homppage_top2 = (LinearLayout) mView.findViewById(R.id.homppage_top2);
		homppage_top1 = (LinearLayout) mView.findViewById(R.id.homppage_top1);
		scroll_top = (RelativeLayout) mView.findViewById(R.id.scroll_top);

		renqi = (RelativeLayout) mView.findViewById(R.id.renqi);
		shengyu = (RelativeLayout) mView.findViewById(R.id.shengyu);
		zuixin = (RelativeLayout) mView.findViewById(R.id.zuixin);
		zongxu_down = (RelativeLayout) mView.findViewById(R.id.zongxu_down);
		zongxu_up = (RelativeLayout) mView.findViewById(R.id.zongxu_up);
		renqi_tv = (TextView) mView.findViewById(R.id.renqi_tv);
		shengyu_tv = (TextView) mView.findViewById(R.id.shengyu_tv);
		zuixin_tv = (TextView) mView.findViewById(R.id.zuixin_tv);
		zongxu_down_tv = (TextView) mView.findViewById(R.id.zongxu_down_tv);
		zongxu_up_tv = (TextView) mView.findViewById(R.id.zongxu_up_tv);

		renqi.setOnClickListener(this);
		shengyu.setOnClickListener(this);
		zuixin.setOnClickListener(this);
		zongxu_down.setOnClickListener(this);
		zongxu_up.setOnClickListener(this);

		// 自动轮播
		main_vPager = (AutoScrollViewPager) mView.findViewById(R.id.main_vPagers);
		main_vPager.setInterval(4000);
		main_vPager.startAutoScroll();

		// fragmain_refresh = (PullToRefreshScrollView)
		// mView.findViewById(R.id.fragmain_refresh);
		// fragmain_refresh.scrollTo(0, 0);

		main_vPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				for (int i = 0; i < imageViews.length; i++) {
					if (arg0 == i) {
						imageViews[arg0].setBackgroundResource(R.drawable.dot1);
					}
					if (arg0 != i) {
						imageViews[i].setBackgroundResource(R.drawable.dot0);
					}
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		fragmain_new_imgbt = (LinearLayout) mView.findViewById(R.id.fragmain_new_imgbt);
		// freghome_recommend_imabt = (ImageButton)
		// mView.findViewById(R.id.freghome_recommend_imabt);
		// 晒单
		/*
		 * fragmain_shar_imgbt = (ImageButton)
		 * mView.findViewById(R.id.fragmain_shar_imgbt); shar1_img = V.f(mView,
		 * R.id.fraghome_shar1_img); shar2_img = V.f(mView,
		 * R.id.fraghome_shar2_img); shar3_img = V.f(mView,
		 * R.id.fraghome_shar3_img);
		 * 
		 * shar1_tx = V.f(mView, R.id.fraghome_shar1_tx); shar2_tx = V.f(mView,
		 * R.id.fraghome_shar2_tx); shar3_tx = V.f(mView,
		 * R.id.fraghome_shar3_tx);
		 * 
		 * xian1_img = V.f(mView, R.id.fraghome_xian1_img); xian2_img =
		 * V.f(mView, R.id.fraghome_xian2_img); xian3_img = V.f(mView,
		 * R.id.fraghome_xian3_img); xian4_img = V.f(mView,
		 * R.id.fraghome_xian4_img); xian5_img = V.f(mView,
		 * R.id.fraghome_xian5_img);
		 */
		// 最新揭晓
		news_lay = V.f(mView, R.id.news_lay);
		myViewPager = (MyViewPager) mView.findViewById(R.id.main_vPager);

		// fraghome_new.setOnClickListener(this);
		// fraghome_list.setOnClickListener(this);
		// fraghome_share.setOnClickListener(this);
		fragmain_new_imgbt.setOnClickListener(this);
		// freghome_recommend_imabt.setOnClickListener(this);
		// fragmain_shar_imgbt.setOnClickListener(this);

		// shar1_img.setOnClickListener(this);
		// shar2_img.setOnClickListener(this);
		// shar3_img.setOnClickListener(this);
		// xian1_img.setOnClickListener(this);
		// xian2_img.setOnClickListener(this);
		// xian3_img.setOnClickListener(this);
		// xian4_img.setOnClickListener(this);
		// xian5_img.setOnClickListener(this);

		group = V.f(mView, R.id.viewGroup);
		group.setPadding(0, 0, 5, -5);

		dialog = new CustomProgressDialog(mContext, "正在加载中...", R.anim.frame);
		dialog.show();
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		base_right_iv.setOnClickListener(this);
		// 初始化悬浮窗背景
		renqi_tv.setBackgroundResource(R.drawable.textview_click_bg_style);
		shengyu_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
		zuixin_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
		zongxu_down_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
		zongxu_up_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
		renqi_tv.setTextColor(Color.RED);
		shengyu_tv.setTextColor(Color.BLACK);
		zuixin_tv.setTextColor(Color.BLACK);
		zongxu_down_tv.setTextColor(Color.BLACK);
		zongxu_up_tv.setTextColor(Color.BLACK);

		// 侧滑
		mSlidingMenu = new SlidingMenu(getActivity());
		mSlidingMenu.setMode(SlidingMenu.LEFT);
		mSlidingMenu.setShadowDrawable(R.color.sliding_shadow);
		mSlidingMenu.setShadowWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth() / 40);
		mSlidingMenu.setBehindOffsetRes(R.dimen.sliding_menu_offset);
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		mSlidingMenu.attachToActivity(getActivity(), SlidingMenu.SLIDING_CONTENT);
		mSlidingMenu.setMenu(R.layout.homepage_sliding);
		slidingmenu_list = (ListView) mSlidingMenu.findViewById(R.id.slidingmenu_list);
		slidingmenu_list.setAdapter(slidingMenuLeftAdapter);
		slidingmenu_list.setOnItemClickListener(this);

		mSlidingMenu.findViewById(R.id.sousuo_goods).setOnClickListener(this);

		// fragmain_refresh.setOnRefreshListener(this);
		inflater = LayoutInflater.from(mContext);
		list = new ArrayList<String>();
		views = new ArrayList<View>();
		mainImage();// 下载轮播图

		// 上滑悬浮
		mReboundScrollView.setOnScrollListener(new OnScrollLisener() {

			@Override
			public void onScroll(int l, int t, int oldl, int oldt) {
				// TODO Auto-generated method stub
				if (flag) {
					flag = false;
					// statusBarHeight = DeviceUtil.getStatusBarHeight();
					// topicBarHeight =
					// getActivity().getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
					topicBarHeight = getActivity().getWindow().findViewById(R.id.homppage_top1).getBottom();
				}
				fun();
			}
		});
		/*
		 * listr = new ArrayList<ListEntity2>(); entities = new
		 * ArrayList<ListEntity>();
		 */
		/*
		 * // 最新揭晓 mAdapter = new NewsAdapter(mContext);
		 * news_lay.setAdapter(mAdapter); news_lay.setOnItemClickListener(this);
		 */

		// xian1_img.setImageResource(R.drawable.xian1);
		// xian2_img.setImageResource(R.drawable.xian2);
		// xian3_img.setImageResource(R.drawable.xian3);
		// xian4_img.setImageResource(R.drawable.xian4);
		// xian5_img.setImageResource(R.drawable.xian5);
		// 人气推荐
		// recommend_recycler.setLayoutManager(new StaggeredGridLayoutManager(1,
		// StaggeredGridLayoutManager.HORIZONTAL));
		recommend();
		// 晒单
		shar();

	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		news();
	}

	/** 最新揭晓 */
	private void news() {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.putOpt("model", "GoodsStatus");
			jsonObject.putOpt("app", "1");
			jsonObject.putOpt("function", "getGoodsStatus");
			jsonObject.putOpt("code", "0,4");
			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		utils.send(HttpMethod.POST, Values.MAIN_RECOMMEND, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {

			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				try {
					JSONObject jsonObject = new JSONObject(arg0.result);
					if (jsonObject.getString("status").equals("1")) {
						Gson gson = new Gson();
						NewsAnnoBean bean = gson.fromJson(arg0.result, NewsAnnoBean.class);
						if (!EmptyUtil.isEmpty(bean.getData().getList())) {
							List<NewsAnnoBean.DataEntity.ListEntity> entity = bean.getData().getList();
							mAdapter.setList(entity);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

				dialog.dismiss();
			}
		});

	}

	@Override
	public void onRefresh(PullToRefreshBase refreshView) {

	}

	private ShowOrderEntity soe1;
	private ShowOrderEntity soe2;
	private ShowOrderEntity soe3;

	// 晒单
	private void shar() {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.putOpt("model", "Share");
			jsonObject.putOpt("app", "1");
			jsonObject.putOpt("function", "getAllShares");
			jsonObject.putOpt("code", "3,,recommend_sort DESC");
			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Mlog.d("人气推荐url=", Values.MAIN_RECOMMEND);
		utils.send(HttpMethod.POST, Values.MAIN_RECOMMEND, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				showToast("网络请求失败");
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				Mlog.d("rerult=", result);
				if (result != null) {
					try {
						JSONObject object = new JSONObject(result);
						if (object.getString("status").equals("1")) {
							// JSONObject objec=new
							// JSONObject(object.getString("data"));
							if (object.getString("data") != null) {
								JSONObject jso = object.getJSONObject("data");
								String s = jso.getString("list");
								// String ss = jso.getString("list");
								// if(s != null && "null".equals(ss)){
								if (!TextUtils.isEmpty(s)) {

									Gson gson = new GsonBuilder().create();
									SharBean image = gson.fromJson(result, new TypeToken<SharBean>() {
									}.getType());

									JSONArray ja = jso.getJSONArray("list");
									if (ja.length() == 1) {
										soe1 = image.getData().getList().get(0);
										JSONArray arra1 = new JSONArray(image.getData().getList().get(0).getFace_img());
										BitmapUtils tUtils = new BitmapUtils(mContext);
										tUtils.display(shar1_img, arra1.getString(0));
										shar1_tx.setText(image.getData().getList().get(0).getShare_title());
									} else if (ja.length() == 2) {
										soe1 = image.getData().getList().get(0);
										soe2 = image.getData().getList().get(1);
										JSONArray arra1 = new JSONArray(image.getData().getList().get(0).getFace_img());
										JSONArray arra2 = new JSONArray(image.getData().getList().get(1).getFace_img());
										BitmapUtils tUtils = new BitmapUtils(mContext);
										tUtils.display(shar1_img, arra1.getString(0));
										BitmapUtils tUtil = new BitmapUtils(mContext);
										tUtil.display(shar2_img, arra2.getString(0));
										shar1_tx.setText(image.getData().getList().get(0).getShare_title());
										shar2_tx.setText(image.getData().getList().get(1).getShare_title());
									} else if (ja.length() == 3) {
										soe1 = image.getData().getList().get(0);
										soe2 = image.getData().getList().get(1);
										soe3 = image.getData().getList().get(2);
										JSONArray arra1 = new JSONArray(image.getData().getList().get(0).getFace_img());
										// JSONArray arra1=new
										// JSONArray(image.getList().get(0).getFace_img());
										JSONArray arra2 = new JSONArray(image.getData().getList().get(1).getFace_img());
										JSONArray arra3 = new JSONArray(image.getData().getList().get(2).getFace_img());
										BitmapUtils tUtils = new BitmapUtils(mContext);
										tUtils.display(shar1_img, arra1.getString(0));
										BitmapUtils tUtil = new BitmapUtils(mContext);
										tUtil.display(shar2_img, arra2.getString(0));
										BitmapUtils tUtil1 = new BitmapUtils(mContext);
										tUtil1.display(shar3_img, arra3.getString(0));

										shar1_tx.setText(image.getData().getList().get(0).getShare_title());
										shar2_tx.setText(image.getData().getList().get(1).getShare_title());
										shar3_tx.setText(image.getData().getList().get(2).getShare_title());
									}

								}
							}
						}

					} catch (JSONException e) {
						e.printStackTrace();
					}

				}
			}
		});
	}

	private void recommend() {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.putOpt("model", "GoodsStatus");
			jsonObject.putOpt("app", "1");
			jsonObject.putOpt("function", "getGoodsStatus");
			jsonObject.putOpt("code", "1,10");
			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		utils.send(HttpMethod.POST, Values.MAIN_RECOMMEND, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				showToast("网络请求失败");
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				Mlog.d("rerult=", result);
				if (result != null) {
					try {
						JSONObject object = new JSONObject(result);
						if (object.getString("status").equals("1")) {
							if (object.getString("data") != null) {

								JSONObject object2 = new JSONObject(object.getString("data"));
								Gson gson = new GsonBuilder().create();
								listr = gson.fromJson(object2.getString("list"), new TypeToken<List<ListEntity2>>() {
								}.getType());
							}
							adapter = new MyAdapters();
							adapter.notifyDataSetChanged();
							recommend_recycler.setAdapter(adapter);
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
		});
	}

	// 轮播图
	private void mainImage() {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("common", "get_carousel");
			jsonObject.putOpt("code", "21");
			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		utils.send(HttpMethod.POST, Values.MAIN_IMAGE, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				Mlog.d("result=" + "网络失败");
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				Mlog.d("result=" + result);
				// Gson gson=new GsonBuilder().create();
				// MainImageBean imageBean=gson.fromJson(result,new
				// TypeToken<MainImageBean>(){}.getType());
				JSONObject object;
				try {
					object = new JSONObject(result);
					if (object.getString("status").equals("1")) {
						JSONArray array = new JSONArray(object.getString("data"));
						for (int i = 0; i < array.length(); i++) {
							list.add(array.getString(i));
						}
						for (int i1 = 0; i1 < list.size(); i1++) {
							item = inflater.inflate(R.layout.item_image, null);
							views.add(item);
						}

						imageViews = new ImageView[views.size()];
						for (int i = 0; i < views.size(); i++) {
							ImageView img = new ImageView(mContext);

							LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
							params.setMargins(4, 10, 4, 10);
							img.setLayoutParams(params);
							imageViews[i] = img;
							if (i == 0) {
								imageViews[i].setBackgroundResource(R.drawable.dot1);
							} else {
								imageViews[i].setBackgroundResource(R.drawable.dot0);
							}
							group.addView(imageViews[i]);
						}

						main_vPager.setAdapter(new imageAdapter(views));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				// dialog.dismiss();
			}
		});
		// main_vPager.setOnPageChangeListener(new ImageListener());
	}

	class imageAdapter extends PagerAdapter {
		private List<View> lists;

		public imageAdapter(List<View> lists) {
			super();
			this.lists = lists;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = lists.get(position);
			item_view = (ImageView) view.findViewById(R.id.item_view);
			ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + list.get(position).toString(), item_view);
			container.addView(lists.get(position));

			return lists.get(position);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(lists.get(position));
		}

	}

	// 人气推荐adapter
	class MyAdapters extends RecyclerView.Adapter<MyAdapters.MyViewHolder> {

		class MyViewHolder extends ViewHolder {
			LinearLayout layout;
			TextView textView, count, total, left, itemhome_title_name;
			ImageView image;
			ProgressBar bar;

			public MyViewHolder(View arg0) {
				super(arg0);
				layout = (LinearLayout) arg0.findViewById(R.id.itemhome_recom_lay);
				textView = (TextView) arg0.findViewById(R.id.itemhome_price_tx);
				itemhome_title_name = (TextView) arg0.findViewById(R.id.itemhome_title_name);
				image = (ImageView) arg0.findViewById(R.id.itemhome_image_img);
				bar = (ProgressBar) arg0.findViewById(R.id.itemhome_count_Pro);

				count = (TextView) arg0.findViewById(R.id.itemhome_count_tx);
				total = (TextView) arg0.findViewById(R.id.itemhome_total_tx);
				left = (TextView) arg0.findViewById(R.id.itemhome_left_tx);
			}
		}

		@Override
		public int getItemCount() {
			return listr.size();
		}

		@Override
		public void onBindViewHolder(MyViewHolder holder, final int arg1) {
			int left = TransferTool.Strtoint(listr.get(arg1).getGoods_price(), ".") - listr.get(arg1).getSurplus();
			String total = TransferTool.getString(listr.get(arg1).getGoods_price(), ".");
			holder.textView.setText(listr.get(arg1).getGoods_price());
			holder.count.setText(listr.get(arg1).getSurplus() + "");
			holder.total.setText(total);
			holder.left.setText(TransferTool.toString(left));
			holder.itemhome_title_name.setText("(第" + listr.get(arg1).getExpect()
					+ mContext.getResources().getString(R.string.expect) + ")" + listr.get(arg1).getGoods_name());
			// Double double1 = (double) (listr.get(arg1).getSurplus()) /
			// (TransferTool.toInt(total) / 100);
			Double d = (double) ((listr.get(arg1).getSurplus() / Double.parseDouble(listr.get(arg1).getGoods_price()))
					* 100.00);

			// int pro = d.intValue();
			if (d < 1 && d > 0) {
				holder.bar.setProgress(1);
			} else {
				holder.bar.setProgress(d.intValue());
			}
			BitmapUtils tUtils = new BitmapUtils(mContext);
			tUtils.display(holder.image, listr.get(arg1).getImg_n());
			holder.layout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO 跳转到商品详情
					Intent i = new Intent(mContext, ProductDeTimeActivity.class);
					i.putExtra("listEntity2", listr.get(arg1));
					startActivity(i);
				}
			});
		}

		@Override
		public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
			MyViewHolder holder = new MyViewHolder(
					LayoutInflater.from(mContext).inflate(R.layout.item_home_recom, arg0, false));
			return holder;
		}
	}

	// 接口监听跳转
	public interface SetClickListener {
		void onNewClick();

		void onShopsClick();

		void onNIClick();

		void onUser();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 侧滑
		case R.id.homepage_left_iv:
			if (mSlidingMenu.isMenuShowing()) {
				mSlidingMenu.showContent();
			} else {
				mSlidingMenu.showMenu();
			}
			break;
		// 搜索
		case R.id.homepage_right_tv:
			Intent intent = new Intent(mContext, SearchActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
		// 点击跳转到最新揭晓
		case R.id.fragmain_new_imgbt:
			intent = new Intent(mContext, MainActivity.class);
			intent.putExtra("tag", 1);
			startActivity(intent);
			break;
		case R.id.renqi:
			Log.i("人气", "renqi");
			renqi_tv.setBackgroundResource(R.drawable.textview_click_bg_style);
			shengyu_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zuixin_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zongxu_down_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zongxu_up_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			renqi_tv.setTextColor(Color.RED);
			shengyu_tv.setTextColor(Color.BLACK);
			zuixin_tv.setTextColor(Color.BLACK);
			zongxu_down_tv.setTextColor(Color.BLACK);
			zongxu_up_tv.setTextColor(Color.BLACK);
			break;
		case R.id.shengyu:
			renqi_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			shengyu_tv.setBackgroundResource(R.drawable.textview_click_bg_style);
			zuixin_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zongxu_down_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zongxu_up_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);

			renqi_tv.setTextColor(Color.BLACK);
			shengyu_tv.setTextColor(Color.RED);
			zuixin_tv.setTextColor(Color.BLACK);
			zongxu_down_tv.setTextColor(Color.BLACK);
			zongxu_up_tv.setTextColor(Color.BLACK);
			break;
		case R.id.zuixin:
			renqi_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			shengyu_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zuixin_tv.setBackgroundResource(R.drawable.textview_click_bg_style);
			zongxu_down_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zongxu_up_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			renqi_tv.setTextColor(Color.BLACK);
			shengyu_tv.setTextColor(Color.BLACK);
			zuixin_tv.setTextColor(Color.RED);
			zongxu_down_tv.setTextColor(Color.BLACK);
			zongxu_up_tv.setTextColor(Color.BLACK);
			break;
		case R.id.zongxu_down:
			renqi_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			shengyu_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zuixin_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zongxu_down_tv.setBackgroundResource(R.drawable.textview_click_bg_style);
			zongxu_up_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			renqi_tv.setTextColor(Color.BLACK);
			shengyu_tv.setTextColor(Color.BLACK);
			zuixin_tv.setTextColor(Color.BLACK);
			zongxu_down_tv.setTextColor(Color.RED);
			zongxu_up_tv.setTextColor(Color.BLACK);
			break;
		case R.id.zongxu_up:
			renqi_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			shengyu_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zuixin_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zongxu_down_tv.setBackgroundResource(R.drawable.textview_noclick_bg_style);
			zongxu_up_tv.setBackgroundResource(R.drawable.textview_click_bg_style);
			renqi_tv.setTextColor(Color.BLACK);
			shengyu_tv.setTextColor(Color.BLACK);
			zuixin_tv.setTextColor(Color.BLACK);
			zongxu_down_tv.setTextColor(Color.BLACK);
			zongxu_up_tv.setTextColor(Color.RED);
			break;
		}
	}

	// 侧滑菜单点击项
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		/*
		 * if (getActivity() instanceof SetClickListener) { ((SetClickListener)
		 * getActivity()).onNewClick(); }
		 */
		Intent intent = new Intent(getActivity(), SlidingMenuActivity.class);

		switch (position) {
		case 0:
			intent.putExtra("sliding", "数码影音");
			startActivity(intent);
			finish();
			break;
		case 1:
			intent.putExtra("sliding", "电脑办公");
			startActivity(intent);
			finish();
			break;
		case 2:
			intent.putExtra("sliding", "智能硬件");
			startActivity(intent);
			finish();
			break;
		case 3:
			intent.putExtra("sliding", "时尚家居");
			startActivity(intent);
			finish();
			break;
		case 4:
			intent.putExtra("sliding", "食品饮料");
			startActivity(intent);
			finish();
			break;
		case 5:
			intent.putExtra("sliding", "新手专区");
			startActivity(intent);
			finish();
			break;
		case 6:
			intent.putExtra("sliding", "任性专区");
			startActivity(intent);
			finish();
			break;
		case 7:
			intent.putExtra("sliding", "其他商品");
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}
	}

	private void fun() {
		int[] location = new int[2];
		homppage_top2.getLocationOnScreen(location);
		int y = location[1]; // homppage_top2的高度

		int height = statusBarHeight + topicBarHeight;
		if (height > y) {
			if (inInclude.getParent() != homppage_top1) {
				homppage_top2.removeView(inInclude);
				homppage_top1.addView(inInclude);
			}
		} else {
			if (inInclude.getParent() != homppage_top2) {
				homppage_top1.removeView(inInclude);
				homppage_top2.addView(inInclude);
			}
		}
	}

	private Boolean exit = false;
	private View.OnKeyListener backlistener = new OnKeyListener() {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			// TODO Auto-generated method stub
			if (event.getAction() == KeyEvent.ACTION_DOWN) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					if (mSlidingMenu.isMenuShowing()) {
						mSlidingMenu.showContent();
					} else {
						if (!exit) {
							Toast.makeText(getActivity(),
									"再按一次退出" + getActivity().getResources().getString(R.string.app_name),
									Toast.LENGTH_LONG).show();
							exit = true;
						} else {
							finish();
						}
					}
					return true;
				}
			}
			return false;
		}
	};

}
